#pragma once
#include "Template.h"
#include "CouplesList.h"
#include "PersonList.h"
#include "InterestTable.h"
#include <iostream>

class Dating
{
private:
	CouplesList cl;
	Couple c;
	InterestTable it;

public:
	Dating(void){}
	~Dating(void){}

	Element<Person> *search(Element<Person> *p, List<Person> &pl, int commonInterests)
	{
		int c = 0;
		Element<string> *pInterest = p->element_info.interestList.head;
		Element<Person> *e = pl.head;

		if(e == nullptr)
			return nullptr;
		else
		{
			while(e != nullptr)
			{
				cout << p->element_info.name << "-" << e->element_info.name << endl;
				while(pInterest != nullptr)
				{
					cout << "*" << pInterest->element_info << endl;
					if(it.searchForInterests(pInterest, e->element_info.interestList, commonInterests))
					{
						c++;
						cout << c << endl;
						if(c >= commonInterests)
							return e;
						pInterest = pInterest->next;
					}
					else
						pInterest = pInterest->next;
				}
				pInterest = p->element_info.interestList.head;
				e = e->next;
				c = 0;
				cout << "__________________________" << endl << endl;
			}
		}
		return nullptr;
	}

	void removeFromList(List <Person> &list)
	{
		Element<Person> *n = list.head;
		Element<Person> *prev = nullptr;
		if(n == nullptr)
			return;
		while(n != nullptr)
		{
			if(n->element_info.single == false)
			{
				if(prev == nullptr)
				{
					if(n == list.tail)
						list.head = list.tail = nullptr;
					else
						list.head = list.head->next;
				}
				else
				{
					if(n  ==  list.tail)
					{
						list.tail = prev;
						prev->next = nullptr;
					}
					else
						prev->next = n->next;
				}
				delete n;
				return;
			}
			else
			{
				prev = n;
				n = n->next;
			}
		}
	}

	int getListSize(List<Person> l)
	{
		Element<Person> *p = l.head;
		int size = 0;
		while(p != nullptr)
		{
			size++;
			p = p->next;
		}
		return size;
	}

	void printList(List<Person> &girls, List<Person> &boys)
	{
		Element<Person> *boy = boys.head;
		Element<Person> *girl = girls.head;
		cout << "Boyslist: " << endl;
		while(boy != nullptr)
		{
			cout << boy->element_info.name << endl;
			boy = boy->next;
		}
		cout << endl << "Girlslist: " << endl;
		while(girl != nullptr)
		{
			cout << girl->element_info.name << endl;
			girl = girl->next;
		}
		cout << endl;
	}
	void findMatch(int commonInterests, List<Person> &girls, List<Person> &boys)
	{
		int i = 0;
		int size = getListSize(girls);
		Element<Person> *boy = boys.head;
		Element<Person> *girl = girls.head;
		while(i < size)
		{
			while(girl != nullptr)
			{
				Element<Person> *p = search(girl, boys, commonInterests);
				if(p != nullptr)
				{
					cout << endl << p->element_info.name << " and " << girl->element_info.name << " are added to couplesList" 
						<< endl << "__________________________" << endl << endl;
					girl->element_info.single = false;
					p->element_info.single = false;
					c.P1 = girl->element_info;
					c.P2 = p->element_info;
					cl.add(c);
					removeFromList(boys);
					removeFromList(girls);
					break;
				}
				girl = girl->next;
				cout << "There's no suitable partner" << endl << "_______________________" << endl << endl;
			}
			i++;
			boy = boys.head;
			girl = girls.head;
		}
		cout << endl;
		cl.printList();
		cout << "Persons without partners: " << endl << endl;
		printList(girls, boys);
	}
};